package com.cloudcc.mobile.manager;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.model.DynamicModel;
import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppConfig;
import com.cloudcc.mobile.entity.chatter.ChatterEventType;
import com.cloudcc.mobile.entity.schedule.ScheduleEntity;
import com.cloudcc.mobile.util.PreferenceUtils;
import com.google.gson.reflect.TypeToken;

public class CacheManager {

	private static CacheManager instance;
	private Context mContext;
	private final String xml = AppConfig.XML_CACHE;

	private CacheManager(Context context) {
		super();
		this.mContext = context;
	}

	public static CacheManager getInstance() {
		if (instance == null) {
			instance = new CacheManager(CApplication.getApp());
		}
		return instance;
	}
	
	/**
	 * 删除所有的用户缓存信息
	 */
	public void clear() {
		PreferenceUtils.clear(mContext, xml);
	}

	/**
	 * 缓存菜单
	 * 
	 * @param menus
	 */
	public void cacheMenu(List<MenuModel> menus) {
		String json = GsonUtil.List2Json(menus);
		PreferenceUtils.commitString(mContext, xml, IKey.MENUS, json);
	}
	/**
	 * 忽略的选项卡
	 * 
	 * @param menus
	 */
	public void cacheIgnoreMenu(List<MenuTabModel> menus) {
		String json = GsonUtil.List2Json(menus);
		PreferenceUtils.commitString(mContext, xml, IKey.MENUS_IGNORES, json);
	}
	/**
	 * 获得忽略的选项卡列表
	 * @return
	 */
	public List<MenuTabModel> getIgnoreMenu() {
		Type type = new TypeToken<List<MenuTabModel>>() {
		}.getType();
		String string = PreferenceUtils
				.getString(mContext, xml, IKey.MENUS_IGNORES, "");
		List<MenuTabModel> list=null;
		try {
			list = GsonUtil.getList(string, type);
		} catch (com.google.gson.JsonSyntaxException e) {
		}
		return list;
	}

	/** 
	* 获得缓存的menu
	* @return
	*/ 
	public List<MenuModel> getMenu() {
		Type type = new TypeToken<List<MenuModel>>() {
		}.getType();
		String string = PreferenceUtils
				.getString(mContext, xml, IKey.MENUS, "");
		List<MenuModel> list=null;
		try {
			list = GsonUtil.getList(string, type);
		} catch (com.google.gson.JsonSyntaxException e) {
		}
		return list;
	}

	public void cacheDynamic(String key, List<DynamicModel> data) {
		String json = GsonUtil.List2Json(data);
		PreferenceUtils.commitString(mContext, xml, key, json);
	}
	
	
	/** 缓存当前月的数据
	 * @param key
	 * @param data
	 */
	public void cacheThisMonthSchedule(String key, List<ScheduleEntity> data){
		String json = GsonUtil.List2Json(data);
		PreferenceUtils.commitString(mContext, xml, key, json);
	}
	
	/** 获得缓存的 当前月份的日程数据
	 * @param key
	 * @return
	 */
	public List<ScheduleEntity> getThisMonthScheduleByCache(String key){
		String json = PreferenceUtils.getString(mContext, xml, key,
				StringUtils.EMPTY);
		Type type = new TypeToken<List<ScheduleEntity>>() {
		}.getType();
		List<ScheduleEntity> list;
		try {
			list = GsonUtil.json2List(json, type);
		} catch (com.google.gson.JsonSyntaxException e) {
			list = new ArrayList<ScheduleEntity>();
		}
		return list;
	}
	

	public List<DynamicModel> getDynamic(String key) {
		String json = PreferenceUtils.getString(mContext, xml, key,
				StringUtils.EMPTY);
		Type type = new TypeToken<List<DynamicModel>>() {
		}.getType();
		List<DynamicModel> list;
		try {
			list = GsonUtil.json2List(json, type);
		} catch (com.google.gson.JsonSyntaxException e) {
			list = new ArrayList<DynamicModel>();
		}

		return list;
	}
	
	
	/**缓存 chatter event types
	 * @param data
	 */
	public void cacheEventTypes(List<ChatterEventType> data){
		String json = GsonUtil.List2Json(data);
		PreferenceUtils.commitString(mContext, xml, IKey.CHATTER_EVENTTYPES, json);
	}
	/**
	 * 获取 chatter event types 
	 * @return
	 */
	public List<ChatterEventType> getEventTypes(){
		String json = PreferenceUtils.getString(mContext, xml, IKey.CHATTER_EVENTTYPES,
				StringUtils.EMPTY);
		Type type = new TypeToken<List<ChatterEventType>>() {
		}.getType();
		List<ChatterEventType> list;
		try {
			list = GsonUtil.json2List(json, type);
		} catch (com.google.gson.JsonSyntaxException e) {
			list = new ArrayList<ChatterEventType>();
		}
		return list;
	}
	
	

	public interface IKey {

		/**
		 * 菜单
		 */
		String MENUS = "user.menus";
		/**
		 * 不能显示的选项卡
		 */
		String MENUS_IGNORES = "user.menus.ignores";
		/**
		 * 动态 我追随的
		 */
		String DYNAMIC_FOLLOW = "dynamic.follow";
		String DYNAMIC_ME = "dynamic.me";
		String DYNAMIC_MARK = "dynamic.mark";
		String DYNAMIC_company = "dynamic.company";
		
		
		
		
		/**
		 * 日程缓存key
		 */
		String SCHEDULE_MONTH="schedule.month";
		
		/**
		 * 快速回复微贴的 类型
		 */
		String CHATTER_EVENTTYPES="chatter.event.type";
	}

}
